// 获取请求路径
function requestUrl() {
  var ip = window.location.host,
    url = "";
  if (ip.indexOf("127.0.0.1") >= 0 || ip.indexOf("localhost") >= 0) {
    url = "http://114.113.68.14:9000/zhixuan";
  } else if (ip.indexOf("www.") >= 0) {
    url = "/zhixuan";
  } else {
    url = "/zhixuan";
  }
  return url;
}
// 获取cookie
function getCookie(cname) {
  var name = cname + "=";
  var ca = document.cookie.split(";");
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i].trim();
    if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
  }
  return "";
}
// 添加默认图片
function getDefaultImg(img){
  var image = "static/ffyxq/images/noupload.png";
  if (img) {
    image = getImgUrl() + img;
  } else {
    image = "static/ffyxq/images/noupload.png";
  }
  return image;
}
//获取图片路径
function getImgUrl() {
  var ip = window.location.host,
    url = "";
  if (ip.indexOf("127.0.0.1") >= 0 || ip.indexOf("localhost") >= 0) {
    url = "http://114.113.68.14:9000/image/";
  } else if (ip.indexOf("www.") >= 0) {
    url = "http://image.3dzhixuan.com/image/";
  } else {
    url = "http://" + ip + "/image/";
  }
  return url;
}
// 获取参数
function getParams(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}
// 数字大写转小写
function formNumber(num){
  var number = typeof(num) === 'number'? num:parseInt(num);
  var cn = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  var result = '';
  for(var i=0;i<cn.length;i++){
    if(i == number){
      result = cn[i];
    }
  }
  return result;
}

// 转化时间
function forDate(time) {
  var timestamp = (new Date(time) * 1) / 1000;
  function zeroize(num) {
    return (String(num).length == 1 ? "0" : "") + num;
  }
  var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
  var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
  var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
  var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象
  var Y = tmDate.getFullYear(),
    m = tmDate.getMonth() + 1,
    d = tmDate.getDate();
  var H = tmDate.getHours(),
    i = tmDate.getMinutes(),
    s = tmDate.getSeconds();
  if (timestampDiff < 60) {
    // 一分钟以内
    return "刚刚";
  } else if (timestampDiff < 3600) {
    // 一小时前之内
    return Math.floor(timestampDiff / 60) + "分钟前";
  } else if (
    curDate.getFullYear() == Y &&
    curDate.getMonth() + 1 == m &&
    curDate.getDate() == d
  ) {
    return "今天" + zeroize(H) + ":" + zeroize(i);
  } else {
    var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
    if (
      newDate.getFullYear() == Y &&
      newDate.getMonth() + 1 == m &&
      newDate.getDate() == d
    ) {
      return "昨天" + zeroize(H) + ":" + zeroize(i);
    } else if (curDate.getFullYear() == Y) {
      return (
        zeroize(m) + "月" + zeroize(d) + "日 " + zeroize(H) + ":" + zeroize(i)
      );
    } else if (timestampDiff < 259200000) {
      return Math.floor(timestampDiff / 86400000) + "天前";
    } else {
      return (
        Y +
        "年" +
        zeroize(m) +
        "月" +
        zeroize(d) +
        "日 " +
        zeroize(H) +
        ":" +
        zeroize(i)
      );
    }
  }
}
// 判断浏览器类别
function IEVersion() {
  var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
  var isIE =
    userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
  var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
  var isIE11 =
    userAgent.indexOf("Trident") > -1 && userAgent.indexOf("rv:11.0") > -1;
  if (isIE) {
    var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
    reIE.test(userAgent);
    var fIEVersion = parseFloat(RegExp["$1"]);
    if (fIEVersion == 7) {
      return 7;
    } else if (fIEVersion == 8) {
      return 8;
    } else if (fIEVersion == 9) {
      return 9;
    } else if (fIEVersion == 10) {
      return 10;
    } else {
      return 6; //IE版本<=7
    }
  } else if (isEdge) {
    return "edge"; //edge
  } else if (isIE11) {
    return 11; //IE11
  } else {
    return -1; //不是ie浏览器
  }
}
// 封装请求
function ajax(url, method, query, back, async) {
  //url:请求地址 method：请求方式 query：请求参数 back：回调函数 async:是否异步请求
  var sync = true;
  if (async != undefined) {
    sync = async;
  } else {
    sync = true;
  }
  var timestamp = new Date() * 1;
  var source = "web";
  var token = "";
  if (!getCookie("signature")) {
    token = "";
  } else {
    token = getCookie("signature");
  }
  var sign = hex_md5(timestamp + source + token + url);
  var headers = {
    timestamp: timestamp,
    source: source,
    token: token,
    sign: sign,
    "Content-Type": "application/json"
  };
  $.ajax({
    url: requestUrl() + url,
    type: method, //方法类型
    async: sync,
    headers: headers,
    data: JSON.stringify(query),
    success: function(data) {
      back(data);
    }
  });
}
